package app.android.kdictionary.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import app.android.kdictionary.R;
import app.android.kdictionary.data.SearchFormData;

// 메뉴 리스트 adapter
public class SearchFormAdapter extends BaseAdapter {
	private String[] strPicker1;
	private String[] strPicker1_o1;
	private String[] strPicker1_o2;
	private String[] strPicker2;
	
	private ArrayList<SearchFormData> mData = new ArrayList<SearchFormData>();
	
	private LayoutInflater inflater;
	private Context mContext;
	
	public SearchFormAdapter(LayoutInflater inflater, Context context) {
		this.inflater = inflater;
		mContext = context;
		
		strPicker1 = mContext.getResources().getStringArray(R.array.picker_list_1);
		strPicker1_o1 = mContext.getResources().getStringArray(R.array.picker_option_list_1);
		strPicker1_o2 = mContext.getResources().getStringArray(R.array.picker_option_list_2);
		strPicker2 = mContext.getResources().getStringArray(R.array.picker_list_2);
	}

	public void addItem(final SearchFormData item) {
		if(mData.size()>=3) {
			return ;
		}
        mData.add(item);
        notifyDataSetChanged();
    }
	
	public void removeLastItem() {
		if(mData.size()>1) {
			mData.remove(mData.size()-1);
		}
	}
	
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public SearchFormData getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	SearchFormHolder holder = null;
	    if (convertView == null) {
	    	convertView = inflater.inflate(R.layout.multiple_list_01_item, null);
	    	holder = new SearchFormHolder();
	        holder.text1 = (TextView)convertView.findViewById(R.id.label_text1);
	        holder.text2 = (TextView)convertView.findViewById(R.id.label_text2);
	        holder.text3 = (TextView)convertView.findViewById(R.id.label_text3);
	        holder.text4 = (TextView)convertView.findViewById(R.id.label_text4);
	    	convertView.setTag(holder);
	    } else {
	        holder = (SearchFormHolder)convertView.getTag();
	    }
	    
	    holder.text1.setText(strPicker1[mData.get(position).getPicker1()]+",");
	    if( mData.get(position).getPicker1()==1 ) {
	    	holder.text2.setText(strPicker1_o1[mData.get(position).getPicker1_o()]+",");	
	    }
	    else if( mData.get(position).getPicker1()==13 ) {
	    	holder.text2.setText(strPicker1_o2[mData.get(position).getPicker1_o()]+",");	
	    }
	    else {
	    	holder.text2.setText("");
	    }
	    holder.text3.setText(strPicker2[mData.get(position).getPicker2()]);
	    holder.text4.setText(mData.get(position).getText());
	    return convertView;
    }
}

